﻿Imports Mysoft.Report.Page
Imports System.Xml
Imports MySoft.Map.Data
Imports MySoft.Map.Application.Security.User
Imports System.Data
Imports System.Text
Imports System.Web
Imports System.Configuration


Partial Public Class Desk_Report
    Inherits MySoft.Map.Application.Security.AppPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strProjGUID As String
        If Not IsPostBack Then

            If Not Request.QueryString("ProjGUID") Is Nothing Then
                strProjGUID = Request.QueryString("ProjGUID")
                initWsSession()
                initWsApplication()

                Bind_Data()
            End If

            'Bind_Data(strProjGUID)
        End If

    End Sub


#Region " 私有函数 "
    '初始化ws所需要使用到的session信息
    Private Function initWsSession() As String
        Dim sql As String
        sql = "SELECT UserCode FROM myUser WHERE UserGUID='" + HttpContext.Current.Session("UserGUID") + "'"
        Session("UserCode") = MyDB.GetDataItemString(sql)

        sql = "SELECT Password FROM myUser WHERE UserGUID='" + HttpContext.Current.Session("UserGUID") + "'"
        Session("UserPass") = MyDB.GetDataItemString(sql)

        'Session("Scope") = Request.QueryString("scope").ToString
        Session("Scope") = "0101"
    End Function

    '初始化ws所需要使用的application信息
    Private Function initWsApplication() As String
        If HttpContext.Current.Application("MyRptServices") Is Nothing Then
            If Not ConfigurationSettings.AppSettings("MyRptServices") Is Nothing Then
                HttpContext.Current.Application("MyRptServices") = ConfigurationSettings.AppSettings("MyRptServices")
            Else
                HttpContext.Current.Application("MyRptServices") = ""
            End If
        End If

        If HttpContext.Current.Application("MyRptAppKey") Is Nothing Then
            If Not ConfigurationSettings.AppSettings("MyRptAppKey") Is Nothing Then
                HttpContext.Current.Application("MyRptAppKey") = ConfigurationSettings.AppSettings("MyRptAppKey")
            Else
                HttpContext.Current.Application("MyRptAppKey") = ""
            End If
        End If
    End Function

    Private Sub Bind_Data()
        Dim strSql As String
        Dim i, j As Integer
        Dim dt, dtDtl As DataTable
        Dim strwhereparam As String
        Dim sbtable As New StringBuilder
        Dim sbtabledtl As New StringBuilder
        '只针对售楼
        strwhereparam = getGrpWhereParam(Session("UserGUID"), "0101")

        strSql = " SELECT grpid, grpcname, grpclass ,grpguid" & _
                 " FROM rptgroup " & _
                 " WHERE (grpclass > 2 and linkproduct='0101'" + strwhereparam & " ) " & _
                 " and rptgroup.grpcname not in ('屏幕报表','套打报表','活动信函') " & _
                 " ORDER BY grpclass asc ,grpcname asc"
        '" OR ( grpclass = 2 AND linkproduct='0101') " & _
        dt = MyDB.GetDataTable(strSql)

        If dt.Rows.Count > 0 Then
            '制作标题类别
            sbtable.Append("<tr style='PADDING-TOP: 5px;'>")
            For i = 0 To dt.Rows.Count - 1
                sbtable.Append("<td style='padding-left:10px;'><span style='FONT-WEIGHT: bold'>" & dt.Rows(i).Item("grpcname").ToString & "</span></td>")
            Next
            sbtable.Append("</tr>")
            '制作子类
            sbtable.Append("<tr style='PADDING-TOP: 5px;'>")
            For i = 0 To dt.Rows.Count - 1
                sbtable.Append("<td style='padding-left:25px;' valign='top'>")
                dtDtl = getDtlList(dt.Rows(i).Item("grpid").ToString)
                If dtDtl.Rows.Count > 0 Then
                    For j = 0 To dtDtl.Rows.Count - 1
                        sbtabledtl.Append("<table cellSpacing='0' cellPadding='0' border='0' width='100%' style='TABLE-LAYOUT: fixed;'>")
                        sbtabledtl.Append("<tr height='22px'><td><a href=""#"" onclick=""PreviewReport('" & dtDtl.Rows(j).Item("rptid").ToString & "')"">" & dtDtl.Rows(j).Item("rptcname").ToString & "</a></td></tr>")
                        sbtabledtl.Append("</table>")
                    Next
                    sbtable.Append(sbtabledtl.ToString)
                    sbtabledtl.Length = 0
                End If
                sbtable.Append("</td>")
            Next
        Else
            sbtable.Append("<tr><td></td></tr>")
        End If

        ltlWebpart.Text = sbtable.ToString
    End Sub

    Private Function getDtlList(ByVal strGrpId As String) As DataTable
        Dim sql As String
        Dim dt As DataTable
        Dim i As Integer
        Dim userguid As String
        Dim sqlwhereparam As String
        Dim rptid As String

        userguid = Session("UserGUID")

        sqlwhereparam = getRptList(userguid, "0101")

        sql = "SELECT rptid," & _
                 "rptcname," & _
                 "rptexplain, " & _
                 "rptgroup," & _
                 "grpguid," & _
                 "creator," & _
                 "layerid," & _
                 "signusername," & _
                 "signuserguid," & _
                 "0 as ismodi ," & _
                 "0 as isprn " & _
                 " FROM rptdetail" & _
                 " WHERE rptgroup = '" & strGrpId.Replace("'", "''") & "'" + sqlwhereparam & _
                 " ORDER BY (select rptcname from rptdetail r where layerid = substring(rptdetail.layerid, 0, charindex('.', rptdetail.layerid + '.'))), layerid"
        dt = MyDB.GetDataTable(sql)

        For i = 0 To dt.Rows.Count - 1
            Dim sqltmp As String
            sqltmp = "select rptid from rptdetail where layerid='" + dt.Rows(i).Item("layerid").ToString.Substring(0, 5) + "'"
            rptid = MyDB.GetDataItemString(sqltmp)


            '报表允许编辑权限
            sql = "select su.UserGUID from myStationUser su JOIN myStationRights sr ON su.StationGUID=sr.StationGUID where su.userguid='" + userguid.Replace("'", "''") + "' and sr.actioncode='02' and sr.objecttype='" + rptid + "'"
            If MyDB.GetRowsCount(sql) >= 1 Then
                dt.Rows(i).Item("ismodi") = 1
            End If

            'isprn = 0 没有打印及导出权限
            'isprn = 1 有打印，没有导出权限
            'isprn = 2 没有打印，有导出权限
            'isprn = 3 有打印及导出权限
            '报表允许打印权限
            sql = "select su.UserGUID from myStationUser su JOIN myStationRights sr ON su.StationGUID=sr.StationGUID where su.userguid='" + userguid.Replace("'", "''") + "' and sr.actioncode='03' and sr.objecttype='" + rptid + "'"
            If MyDB.GetRowsCount(sql) > 0 Then
                dt.Rows(i).Item("isprn") = dt.Rows(i).Item("isprn") + 1
            End If

            '报表允许导出数据权限
            sql = "select su.UserGUID from myStationUser su JOIN myStationRights sr ON su.StationGUID=sr.StationGUID where su.userguid='" + userguid.Replace("'", "''") + "' and sr.actioncode='04' and sr.objecttype='" + rptid + "'"
            If MyDB.GetRowsCount(sql) > 0 Then
                dt.Rows(i).Item("isprn") = dt.Rows(i).Item("isprn") + 2
            End If

        Next
        Return dt
    End Function

    Private Function getGrpWhereParam(ByVal userguid As String, ByVal strscope As String) As String
        Dim strrtn As String
        Dim strsql As String
        Dim bright As Boolean

        '显示所有报表
        Return " AND (1=1)"

        'If IsAdmin(userguid) Then
        '    strrtn = " AND (1=1)"
        'Else
        '    '是否具备所有查看或者所有编辑权限
        '    If CheckUserRight(userguid, strscope + "1999", "01") = True Or CheckUserRight(userguid, strscope + "1999", "02") = True Then
        '        bright = True
        '    End If

        '    If bright Then
        '        strrtn = " AND (1=1)"
        '    Else
        '        Dim sql As String
        '        Dim dt As DataTable
        '        sql = " select distinct rptdetail.rptgroup " & _
        '                        " from rptdetail " & _
        '                        " join myStationRights on myStationRights.ObjectType=rptdetail.rptid " & _
        '                        " join myStationUser on myStationUser.StationGUID=myStationRights.StationGUID " & _
        '                        " where myStationRights.actioncode in ('00','02') and " & _
        '                        " myStationUser.userguid='" + userguid.Replace("'", "''") + "' "
        '        dt = MyDB.GetDataTable(sql)

        '        Dim i As Integer
        '        Dim j As Integer
        '        Dim grplist As String = ""

        '        For i = 0 To dt.Rows.Count - 1
        '            Dim tmpgrpid As String
        '            tmpgrpid = dt.Rows(i).Item(0).ToString
        '            For j = tmpgrpid.Length / 3 To 3 Step -1
        '                Dim cutgrpid As String
        '                cutgrpid = tmpgrpid.Substring(0, tmpgrpid.Length - (j - 3) * 3)
        '                If grplist.IndexOf(cutgrpid + ",") < 0 Then
        '                    grplist = grplist + "'" + cutgrpid + "',"
        '                End If
        '            Next
        '        Next

        '        If grplist.Length > 0 Then
        '            grplist = grplist.Substring(0, grplist.Length - 1)

        '            strrtn = " AND grpid in (" + grplist + ")"
        '        Else
        '            strrtn = " AND (1=2)"

        '        End If

        '    End If
        'End If

        'Return strrtn
    End Function

    Private Function getRptList(ByVal userguid As String, ByVal strscope As String) As String
        Dim strrtn As String
        Dim bright As Boolean

        '显示所有报表
        Return " AND (1=1)"

        'If IsAdmin(userguid) Then
        '    strrtn = " AND (1=1)"

        '    ''处理系统管理员所有报表查看权限
        '    'isedit.Text = "TRUE"
        '    'isprn.Text = "3"
        'Else

        '    '处理其他用户所有报表查看权限
        '    If CheckUserRight(userguid, strscope + "1999", "01") = True Or CheckUserRight(userguid, strscope + "1999", "02") = True Then
        '        bright = True
        '    End If
        '    If CheckUserRight(userguid, strscope + "1999", "02") = True Then

        '    End If
        '    Dim intisprn = 0
        '    If CheckUserRight(userguid, strscope + "1999", "03") = True Then
        '        intisprn = intisprn + 1
        '    End If
        '    If CheckUserRight(userguid, strscope + "1999", "04") = True Then
        '        intisprn = intisprn + 2
        '    End If

        '    'isprn.Text = intisprn.ToString


        '    If bright Then
        '        strrtn = " "
        '    Else
        '        strrtn = " AND left(layerid,5) in ( " & _
        '                        " select distinct rptdetail.layerid " & _
        '                        " from rptdetail " & _
        '                        " join myStationRights on myStationRights.ObjectType=rptdetail.rptid " & _
        '                        " join myStationUser on myStationUser.StationGUID=myStationRights.StationGUID " & _
        '                        " where myStationRights.actioncode in ('00','02') and " & _
        '                        " myStationUser.userguid='" + userguid + "' " & _
        '                        " )"
        '    End If
        'End If

        'Return strrtn
    End Function

#End Region



End Class